---
title: Filter Operators
---

# Filter Operators

## filter

Filter items emitted by the source Observable by only emitting those that satisfy a specified predicate.

```rx
-0-1-2-3-1-|-

> filter(v => v > 1)

-----2-3---|-
```

```ts
import { from, filter } from "@devicescript/observables"

const obs = from([0, 1, 2, 3, 1])

obs
    // highlight-next-line
    .pipe(filter(v => v > 1))
    .subscribe(v => console.log(v))
```

## debounceTime

Emits a notification from the source Observable
only after a particular time span has passed without another source emission.

```rx
-0-1----3----

> debounceTime(2)

-----0----3-
```

The example below debounce a button down event to one every 5 seconds.

```ts
import { Button } from "@devicescript/core"
import { debounceTime } from "@devicescript/observables"

const button = new Button()

button.down
    // highlight-next-line
    .pipe(debounceTime(5000))
    .subscribe(() => console.log("click"))
```

## throttleTime

Emits a value from the source Observable,
then ignores subsequent source values for duration milliseconds, then repeats this process.

```rx
-0-1-2-3-4-

> throttleTime(2)

-0---2---4-
```

The example below debounce a button down event to one every 5 seconds.

```ts
import { Button } from "@devicescript/core"
import { throttleTime } from "@devicescript/observables"

const button = new Button()

button.down
    // highlight-next-line
    .pipe(throttleTime(1000))
    .subscribe(() => console.log("click"))
```

## auditTime

Ignores source values for duration milliseconds,
then emits the most recent value from the source Observable, then repeats this process

`auditTime` is similar to `throttleTime`, but emits the last value from the silenced time window, instead of the first value.

```rx
-0-1-2-3-4-

> auditTime(2)

-----2--4-
```

The example below debounce a button down event to one every 5 seconds.

```ts
import { Button } from "@devicescript/core"
import { auditTime } from "@devicescript/observables"

const button = new Button()

button.down
    // highlight-next-line
    .pipe(auditTime(1000))
    .subscribe(() => console.log("click"))
```

### threshold

Filters numerical data stream within change threshold.

```rx
-0-1-2-1-0-

> threshold(2)

-0---2---0-
```

The example tracks the temperature returned

```ts
import { Temperature } from "@devicescript/core"
import { threshold } from "@devicescript/observables"

const thermometer = new Temperature()

thermometer.reading
    // highlight-next-line
    .pipe(threshold(2))
    .subscribe(t => console.log(t))
```

## distinctUntilChanged

Returns a result Observable that emits all values pushed by the source observable
if they are distinct in comparison to the last value the result observable emitted.

This function can be used to create user-friendly transformer. For example, thershold is as follows:

```ts skip
export function threshold<T = number>(
    value: number,
    ...
): OperatorFunction<T, T> {
    return distinctUntilChanged<T, number>(
        (l, r) => Math.abs(l - r) < value,
        keySelector
    )
}
```

## Transform operators

### map

Applies a converter function to converts the observed value into a new value.

```rx
-1--2--3-

> map(x => x * x)

-1--4--9-
```

```ts
import { from, map } from "@devicescript/observables"

const obs = from([1, 2, 3])

obs
    // highlight-next-line
    .pipe(map(v => v * v))
    .subscribe(v => console.log(v))
```

### scan

Applies an accumulator (or "reducer function") to each value from the source.

```rx
-1---2---3-

> scan((p, c) => p + c, 0)

-1---3---6-
```

```ts
import { from, scan } from "@devicescript/observables"

const obs = from([1, 2, 3])

obs.pipe(
    // highlight-next-line
    scan((p, c) => p + c, 0)
).subscribe(v => console.log(v))
```

## Signal processing

### ewma

Exponentially weighted moving average is a simple PIR filter
with a gain parameter.
The closer gain to 1 and the more closely the EWMA tracks the original time series.

```ts
import { Temperature } from "@devicescript/core"
import { ewma } from "@devicescript/observables"

const thermometer = new Temperature()

thermometer.reading
    // highlight-next-line
    .pipe(ewma(0.5))
    .subscribe(t => console.log(t))
```

### rollingAverage

A windowed rolling average filter.

```ts
import { Temperature } from "@devicescript/core"
import { rollingAverage } from "@devicescript/observables"
